const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');
const WxParse = require('../../wxParse/wxParse.js');

const app = getApp()
// pages/goods/goods.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fenshu:'',
    usertx:'',
    ys:'',
    th:'',
    imgUrls: [
     
    ],
    TabCur: 0,
    num: 1,
    scrollLeft: 0,
    head: [{
        id: 0,
        heade: '商品详情'
      },
      {
        id: 1,
        heade: '购买记录'
      }
    ],
    detail: [{
        name: '吉安市百货',
        vlalue: '上海'
      },
      {
        name: '吉安市百货',
        vlalue: '上海'
      },
      {
        name: '吉安市百货',
        vlalue: '上海'
      }
    ],
    ren: [{
        img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        name: '上**',
        fen: 1,
        data: '2019/10/12 17:34:12'
      },
      {
        img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        name: '上*',
        fen: 1,
        data: '2019/10/12 17:34:12'
      },
      {
        img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        name: '上*',
        fen: 1,
        data: '2019/10/12 17:34:12'
      },
      {
        img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        name: '上**',
        fen: 1,
        data: '2019/10/12 17:34:12'
      },
      {
        img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        name: '上*',
        fen: 1,
        data: '2019/10/12 17:34:12'
      }
    ],
    goods: {
      oldprice: 3,
      newprice: 1,
      time: '12:12:12',
      name: '洗挂阿萨斯阿萨120g/份',
      desc: '显现果汁 无任何添加剂 轻快一下',
      peploe: '4343',
      ystime: '8.26',
      thtime: '9.26',
      hassail: '200',
      limite: '5000'
    },
    xiangqin: [{
        name: '商家',
        value: '肥猫'
      },
      {
        name: '品牌',
        value: '阿莎'
      },
      {
        name: '规格',
        value: '1盒'
      },
      {
        name: '规格',
        value: '1盒'
      }
    ],
    time3:'',
    detailss: [
      // {
      //   quest: '1.消费者下单',
      //   response: '每日22:00点前下单，第二日送达！'
      // },
      // {
      //   quest: '2.消费者下单',
      //   response: '每日22:00点前下单，第二日送达！'
      // },
      // {
      //   quest: '2.消费者下单',
      //   response: '每日22:00点前下单，第二日送达！'
      // },
      // {
      //   quest: '2.消费者下单',
      //   response: '每日22:00点前下单，第二日送达！'
      // }
    ],
    record: {
      people: '1455',
      num: '2344'
    },
    goodsid:'',
    goodsdetail:{}
  },
  //虚拟销量
  queryUserComm() {
    let that = this
    util.request(api.QueryUserComm, {
    }, 'get', {
      }).then(function (res) {
        console.log(res.data.user.length)
        that.setData({
          usertx: res.data.user,
          fenshu: res.data.user.length
        })
      });
  },
  tabSelect(e) {
    console.log(e)
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  addfast() {
    wx.navigateTo({
      url: '../Order/Order?goodsid=' + this.data.goodsdetail.id,
    })
  },
  add() {
    let that=this
    if (app.globalData.hasLogin == true){
      //  if (that.data.num < 1) {
      // that.setData({
      //   num: that.data.num + 1
      // })
        var token = wx.getStorageSync('token')
        util.request(api.Addcart, {
          commodityId: that.data.goodsid,
          number: that.data.num,
          commImg: that.data.goodsdetail.imgUrl,
          price: that.data.goodsdetail.sellingPrice,
          commodityName: that.data.goodsdetail.name,
          addTime: '',
          updateTime: '',
        }, 'post', {
            'Content-Type': 'application/json',
            'X-Qxmall-Token': wx.getStorageSync('token')
          }).then(function (res) {
            if(res.code==200){
              wx.showToast({
                title: '添加成功',
                icon: 'none'
              })
            }
            if(res.code==405){
              wx.showToast({
                title: '库存不足',
                icon: 'none'
              })
            }
            console.log(res)
         
          });
         
      // }else{
      //   util.showErrorToast('购物车已有该商品');
      // }
    
    }else{
      wx.navigateTo({
        url: '../login/login',
      })
    }
   
  },
  go() {
    console.log('asas')
    wx.switchTab({
      url: '../cart/cart',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.queryUserComm()
    this.setData({
      TabCur: options.t,
      goodsid:options.id
    });
    this.getdetail()
    var time = util.getTime(new Date())
    var time1 = util.getTomorrTime(new Date())
    console.log("111" + time)
    this.setData({
      ys: time,
      th: time1
    })
  },

  getdetail(){
    let that = this
    util.request(api.GoodsDetail,{
      id:that.data.goodsid
    }, 'post', {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
       console.log(res.data)

        let ceshi = res.data.commodity.content
        WxParse.wxParse('article', 'html', ceshi, that, 5); 
      that.setData({
        goodsdetail: res.data.commodity
      });
        var words = res.data.commodity.imgRoll;     //字符串中间已特殊符号隔开
       var words1 = words.split("]"); 
          var words2 = words1[0].split("[");   //转成数组类似php的explode函数
        // console.log(words2)
         let imgurl = that.data.imgUrls
        words2[1] = words2[1].replace(/\"/g, "");
        var bb = words2[1].split(",");
        console.log(bb)
        imgurl.push(bb)
        that.setData({
          imgUrls: bb
        })
        // console.log(that.data.imgUrls)
    });

   
  },

getzhinan(){
  let that = this
  util.request(api.QueryListGuide, {
  },  {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      'X-Qxmall-Token': wx.getStorageSync('token')
    }).then(function (res) {
      console.log(res.data)
      if(res.code==200){
        that.setData({
          detailss: res.data.listGuide
        })
      }
      })
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   this.getzhinan()
   let that=this
    var timer = setInterval(function () {
      var time3 = util.daojishi(new Date())
      that.setData({
        time3:time3
      })
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
  
})